import type { TableColumn } from '@/types'
import { PAGING } from '@/constant'

import { measuringToolsApi } from '@/api'

export function useTableList() {
  const columns = shallowRef<TableColumn<any>[]>([
    { key: uuid(), label: '计量器具编号', prop: 'code' },
    { key: uuid(), label: '计量器具名称', prop: 'name' },
    { key: uuid(), label: '用能单元', prop: 'pointName' },
  ])
  const [loading, setLoading] = useState(false)
  const [tableData, setTableData] = useShallowState<any[]>([])
  const pagination = reactive({
    pageNumber: 1,
    pageSize: PAGING.PAGE_SIZES[0]!,
    total: 0,
  })

  async function queryList(params: any) {
    setLoading(true)
    try {
      const { data } = await measuringToolsApi.selectPointMeasuringToolsPage({
        ...params,
        pageNumber: pagination.pageNumber,
        pageSize: pagination.pageSize,
      })

      setTableData(data.records)
      pagination.total = data.total
    }
    catch (e) {
    }
    finally {
      setLoading(false)
    }
  }
  return {
    columns,
    loading,
    tableData,
    pagination,
    setTableData,
    setLoading,
    queryList,
  }
}
